<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Passport_mgr extends CI_Controller {
	public function ajax_signup() {
		$name = $this->input->post('name');
		$name = trim($name);
		$this->_validate_name($name);
		$email = $this->input->post('email');
		$email = trim($email);
		$this->_validate_email($email);
		$gender = (int) $this->input->post('gender');
		$this->_validate_gender($gender);
		$password = $this->input->post('passwd');
		$password = trim($password);
		$this->_validate_password($password);
		$confirm = $this->input->post('confirm');
		$confirm = trim($confirm);
		$this->_validate_confirm($confirm, $password);
		$agree = (int) $this->input->post('agree');
		$this->_validate_agree($agree);
		
		$this->load->model('user_model');
		if ($this->user_model->exists_name($name)) {
			$ret = array('status' => STATUS_ERROR, 'msg' => '用户名已经存在，请换个用户名注册！');
			echo json_encode($ret); exit;
		}
		if ($this->user_model->exists_email($email)) {
			$ret = array('status' => STATUS_ERROR, 'msg' => '邮箱已经存在，请换个邮箱注册！');
			echo json_encode($ret); exit;
		}
		
		$user = array(
			'email' => $email,
			'name' => $name,
			'gender' => $gender,
			'password' => $this->_gen_password($password),
			'reg_ip' => $this->input->ip_address(),
			'create_time' => date('Y-m-d H:i:s'),
			'level' => USER_LEVEL_NORMAL,
			'status' => DB_STATUS_NORMAL
		);
		$user_id = $this->user_model->add($user);
		$ret = array('status' => STATUS_SUCCESS, 'msg' => '');
		if ($user_id > 0) {
			$user['id'] = $user_id;
			$this->user_model->update_login_info($user_id);
			$this->_set_session($user);
			$ret['data'] = site_url();
		} else {
			$ret = array('status' => STATUS_ERROR, 'msg' => '注册失败请重试！');
		}
		
		echo json_encode($ret);
	}
	
	public function ajax_signin() {
		$ret = array('status' => STATUS_SUCCESS, 'msg' => '');
		$user_id = (int) $this->session->userdata(KEY_USER_ID);
		if ($user_id > 0) {
			$ret['data'] = site_url();
			echo json_encode($ret); exit;
		}
		$name = $this->input->post('name');
		$name = trim($name);
		$this->_validate_name($name);
		$password = $this->input->post('passwd');
		$password = trim($password);
		$this->_validate_password($password);
		
		$this->load->model('user_model');
		$user = $this->user_model->get_by_name_password($name, $this->_gen_password($password));
		if (empty($user)) {
			$ret = array('status' => STATUS_ERROR, 'msg' => '用户名密码错误，请检查后重新输入！');
		} else {
			$ret['data'] = site_url();
			$this->user_model->update_login_info($user['id']);
			$this->_set_session($user);
			$auto_login = (int) $this->input->post('auto_login');
			$this->input->set_cookie(KEY_AUTO_LOGIN, $auto_login, COOKIES_EXPIRE);
			if ($auto_login == 1) {
				$this->input->set_cookie(KEY_USER_NAME, $user['name'], COOKIES_EXPIRE);
				$this->input->set_cookie(KEY_USER_PASSWORD, $user['password'], COOKIES_EXPIRE);
			}
		}
		
		echo json_encode($ret);
	}
	
	public function signout() {
		$this->input->set_cookie(KEY_AUTO_LOGIN, 0);
		$this->session->sess_destroy();
		
		redirect(site_url());
	}
	
	public function ajax_find() {
		$name_or_email = $this->input->post('name_or_email');
		$name_or_email = trim($name_or_email);
		if (preg_match('/@/', $name_or_email)) {
			
		} else {
			
		}
	}
	
	public function ajax_change_password() {
		$user_id = (int) $this->session->userdata(KEY_USER_ID);
		if ($user_id > 0) {
			$ret = array('status' => STATUS_ERROR, 'msg' => '未登录，不能修改密码！');
			echo json_encode($ret); exit;
		}
		$new_password = $this->input->post('new_passwd');
		$this->_validate_password($new_password);
		$confirm = $this->input->post('confirm');
		$this->_validate_confirm($confirm, $new_password);
		
		$this->load->model('user_model');
		$ret = array('status' => STATUS_SUCCESS, 'msg' => '修改成功，请重新<a href="' . site_url('signin.html') . '">登录</a>！');
		$this->user_model->update_password($user_id, $this->_gen_password($new_password));
		$this->input->set_cookie(KEY_AUTO_LOGIN, 0);
		$this->session->sess_destroy();
		
		echo json_encode($ret);
	}
	
	private function _validate_name($name) {
		if (empty($name)) {
			$ret = array('status' => STATUS_ERROR, 'msg' => '用户名不能为空！');
			echo json_encode($ret); exit;
		}
		
		if (!preg_match('/[\w\-\x{4e00}-\x{9fa5}]{2,}/u', $name)) {
			$ret = array('status' => STATUS_ERROR, 'msg' => '用户名格式错误，请重新输入！');
			echo json_encode($ret); exit;
		}
	}
	
	private function _validate_email($email) {
		if (empty($email)) {
			$ret = array('status' => STATUS_ERROR, 'msg' => '邮箱不能为空！');
			echo json_encode($ret); exit;
		}
		
		$this->load->helper('email');
		if (!valid_email($email)) {
			$ret = array('status' => STATUS_ERROR, 'msg' => '邮箱格式错误，请重新输入！');
			echo json_encode($ret); exit;
		}
	}
	
	private function _validate_gender($gender) {
		if (!in_array($gender, array(GENDER_MALE, GENDER_FEMALE, GENDER_OTHER))) {
			$ret = array('status' => STATUS_ERROR, 'msg' => '性别错误，请重新选择！');
			echo json_encode($ret); exit;
		}
	}
	
	private function _validate_password($password) {
		if (empty($password)) {
			$ret = array('status' => STATUS_ERROR, 'msg' => '密码不能为空！');
			echo json_encode($ret); exit;
		}
		
		if (strlen($password) < 6) {
			$ret = array('status' => STATUS_ERROR, 'msg' => '密码太短，不能小于6位！');
			echo json_encode($ret); exit;
		}
	}
	
	private function _validate_confirm($confirm, $password) {
		if (empty($confirm)) {
			$ret = array('status' => STATUS_ERROR, 'msg' => '确认密码不能为空！');
			echo json_encode($ret); exit;
		}
		
		if ($confirm != $password) {
			$ret = array('status' => STATUS_ERROR, 'msg' => '两次输入的密码不一致！');
			echo json_encode($ret); exit;
		}
	}
	
	private function _validate_agree($agree) {
		if ($agree != 1) {
			$ret = array('status' => STATUS_ERROR, 'msg' => '请先接受协议！');
			echo json_encode($ret); exit;
		}
	}
	
	private function _gen_password($str) {
		return empty($str) ? '' : md5($str);
	}
	
	private function _set_session($user) {
		$this->session->set_userdata(KEY_USER_ID, $user['id']);
		$this->session->set_userdata(KEY_USER_EMAIL, $user['email']);
		$this->session->set_userdata(KEY_USER_NAME, $user['name']);
		$this->session->set_userdata(KEY_USER_LEVEL, $user['level']);
	}
}